﻿using System;
using System.Collections.Generic;
using System.Text;
using WMS.IDAL;
using WMS.Entity;
using System.Data.SQLite;
using WMS.DAL.DBUtility;
using System.Data;

namespace WMS.DAL.SQLite
{
    public class UserInfoDB : IUserInfo
    {

        private readonly string insertUserSqlString = "Insert Into UserInfo([UserName],[Password],[CreateDate],[LastLoginDate]) values(@UserName,@Password,@CreateDate,@LastLoginDate)";

        public UserInfoEntity GetUserInfo(int userID)
        {
            return new UserInfoEntity();
        }

        /// <summary>
        /// Get UserInfo
        /// </summary>
        /// <returns></returns>
        public UserInfoEntity GetUserInfo(string userName, string password)
        {
            #region
            UserInfoEntity entity = null;

            string sqlString = "select * from userinfo where username=:username and password=:password";
            SQLiteParameter[] para = new SQLiteParameter[]
            {
                new SQLiteParameter(":username",DbType.String),
                new SQLiteParameter(":password",DbType.String)
            };

            para[0].Value = userName;
            para[1].Value = password;

            SQLiteDataReader reader = SQLiteHelper.ExecuteReader(SQLiteHelper.ConnectionString, CommandType.Text, sqlString, para);
            if (reader.HasRows && reader.Read())
            {
                entity = new UserInfoEntity();
                entity.UserID = Convert.ToInt32(reader["UserID"].ToString());
                entity.UserName = userName;
                entity.CreateDate = Convert.ToDateTime(reader["CreateDate"].ToString());
                entity.LastLoginDate = Convert.ToDateTime(reader["LastLoginDate"].ToString());
            }

            if (reader.IsClosed == false)
                reader.Close();

            return entity;
            #endregion
        }

        /// <summary>
        /// Add User To DB
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddUser(UserInfoEntity entity)
        {
            #region
            SQLiteParameter[] para = new SQLiteParameter[]
            {
                new SQLiteParameter("@UserName",DbType.String),
                new SQLiteParameter("@Password",DbType.String),
                new SQLiteParameter("@CreateDate",DbType.DateTime),
                new SQLiteParameter("@LastLoginDate",DbType.DateTime)
            };

            para[0].Value = entity.UserName;
            para[1].Value = entity.Password;
            para[2].Value = entity.CreateDate;
            para[3].Value = entity.LastLoginDate;

            return SQLiteHelper.ExecuteNonQuery(SQLiteHelper.ConnectionString, CommandType.Text, insertUserSqlString, para);
            #endregion
        }
    }
}
